package biblio;
import java.util.Vector;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CtrNouvParutionGUI.java
 *
 * Created on 15 déc. 2008, 13:16:14
 */

/**
 *
 * @author ardaudg
 */
public class CtrNouvParutionGUIllaume extends javax.swing.JDialog {

private Bibliotheque biblio;
private java.awt.Frame parent_frame;

    /** Creates new form CtrNouvParutionGUI */
    public CtrNouvParutionGUIllaume(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        parent_frame = parent;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }

    public void menuPrincipal(Bibliotheque bib) {
        biblio = bib;
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        issn_label = new javax.swing.JLabel();
        annuler_button = new javax.swing.JButton();
        valider_button = new javax.swing.JButton();
        issn_textfield = new javax.swing.JTextField();
        rechercher_button = new javax.swing.JButton();
        nom_periodique_label = new javax.swing.JLabel();
        nom_periodique_textfield = new javax.swing.JTextField();
        id_parution_label = new javax.swing.JLabel();
        id_parution_textfield = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        mot_cle_jlist = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        auteur_jlist = new javax.swing.JList();
        mots_cles_label = new javax.swing.JLabel();
        auteur_label = new javax.swing.JLabel();
        ajouter_motcle_button = new javax.swing.JButton();
        ajouter_auteur_button = new javax.swing.JButton();
        article_label = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        article_jtable = new javax.swing.JTable();
        ajouter_article_button = new javax.swing.JButton();
        supprimer_article_button = new javax.swing.JButton();
        creer_parution_button = new javax.swing.JButton();
        id_parution_notif_jlabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ajout d'une parution");
        setResizable(false);

        issn_label.setText("Numero ISSN :");

        annuler_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/cancel.png"))); // NOI18N
        annuler_button.setText("Annuler");
        annuler_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                annuler_buttonMousePressed(evt);
            }
        });

        valider_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/tick.png"))); // NOI18N
        valider_button.setText("Valider");
        valider_button.setEnabled(false);
        valider_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                valider_buttonMousePressed(evt);
            }
        });

        rechercher_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/find.png"))); // NOI18N
        rechercher_button.setText("Rechercher");
        rechercher_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                rechercher_buttonMousePressed(evt);
            }
        });

        nom_periodique_label.setText("Nom du périodique:");

        nom_periodique_textfield.setBackground(new java.awt.Color(254, 254, 254));
        nom_periodique_textfield.setForeground(new java.awt.Color(254, 254, 254));
        nom_periodique_textfield.setText("(recherchez un ISSN)");
        nom_periodique_textfield.setToolTipText("Nom du périodique ayant l'ISSN spécifié.");
        nom_periodique_textfield.setEnabled(false);

        id_parution_label.setText("Identifiant de parution:");

        id_parution_textfield.setToolTipText("Entrez ici l'identifiant de parution du périodique spécifié.");
        id_parution_textfield.setEnabled(false);

        mot_cle_jlist.setEnabled(false);
        jScrollPane1.setViewportView(mot_cle_jlist);

        auteur_jlist.setEnabled(false);
        auteur_jlist.setFocusable(false);
        jScrollPane2.setViewportView(auteur_jlist);

        mots_cles_label.setText("Mots clés:");

        auteur_label.setText("Auteurs:");

        ajouter_motcle_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/plus_icon.gif"))); // NOI18N
        ajouter_motcle_button.setText("Ajouter");
        ajouter_motcle_button.setEnabled(false);

        ajouter_auteur_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/plus_icon.gif"))); // NOI18N
        ajouter_auteur_button.setText("Ajouter");
        ajouter_auteur_button.setEnabled(false);
        ajouter_auteur_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ajouter_auteur_buttonMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                ajouter_auteur_buttonMouseReleased(evt);
            }
        });

        article_label.setText("Articles:");

        article_jtable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Titre", "Page"
            }
        ));
        article_jtable.setEnabled(false);
        article_jtable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                article_jtableMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(article_jtable);

        ajouter_article_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/plus_icon.gif"))); // NOI18N
        ajouter_article_button.setText("Ajouter");
        ajouter_article_button.setEnabled(false);
        ajouter_article_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ajouter_article_buttonMousePressed(evt);
            }
        });

        supprimer_article_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/minus_icon.png"))); // NOI18N
        supprimer_article_button.setText("Supprimer");
        supprimer_article_button.setEnabled(false);
        supprimer_article_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                supprimer_article_buttonMousePressed(evt);
            }
        });

        creer_parution_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/arrow_right.png"))); // NOI18N
        creer_parution_button.setText("Créer");
        creer_parution_button.setEnabled(false);
        creer_parution_button.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        creer_parution_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                creer_parution_buttonMousePressed(evt);
            }
        });

        id_parution_notif_jlabel.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(issn_label, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                                .addComponent(nom_periodique_label, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                                .addComponent(id_parution_label, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(id_parution_textfield)
                                .addComponent(nom_periodique_textfield)
                                .addComponent(issn_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(id_parution_notif_jlabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(creer_parution_button)
                                .addComponent(rechercher_button))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(supprimer_article_button)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ajouter_article_button))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addComponent(article_label))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(auteur_label)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(ajouter_auteur_button)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ajouter_motcle_button, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(mots_cles_label))
                            .addGap(21, 21, 21)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(valider_button, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(issn_label)
                    .addComponent(rechercher_button)
                    .addComponent(issn_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nom_periodique_label, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nom_periodique_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(id_parution_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(id_parution_label, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(creer_parution_button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(id_parution_notif_jlabel)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mots_cles_label)
                    .addComponent(article_label)
                    .addComponent(auteur_label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ajouter_motcle_button))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ajouter_article_button)
                                .addComponent(supprimer_article_button))
                            .addComponent(ajouter_auteur_button))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valider_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void annuler_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_annuler_buttonMousePressed
        this.dispose();
}//GEN-LAST:event_annuler_buttonMousePressed

    private void valider_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_valider_buttonMousePressed
        // TODO add your handling code here:
        //on ajoute l'identifiant de parution au périodique

        //on créé la parution et ses détails
}//GEN-LAST:event_valider_buttonMousePressed

    private void rechercher_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rechercher_buttonMousePressed
        // TODO add your handling code here:
        String issn = issn_textfield.getText();
        Periodique period = biblio.unPeriodique(issn);
        
        if (period == null) {
            nom_periodique_textfield.setDisabledTextColor(Color.red);
            nom_periodique_textfield.setText("L'ISSN spécifié n'existe pas.");
        } else {
            nom_periodique_textfield.setText(period.nom());
            issn_textfield.setEnabled(false);
            rechercher_button.setEnabled(false);
            id_parution_textfield.setEnabled(true);
            creer_parution_button.setEnabled(true);



        }

}//GEN-LAST:event_rechercher_buttonMousePressed

    private void ajouter_auteur_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ajouter_auteur_buttonMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_ajouter_auteur_buttonMousePressed

    private void ajouter_article_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ajouter_article_buttonMousePressed
            Vector input= new Vector(2);

            DefaultTableModel model = (DefaultTableModel)article_jtable.getModel(); //on récupère le modèle courant
            model.addRow(input);

            valider_button.setEnabled(true);
    }//GEN-LAST:event_ajouter_article_buttonMousePressed

    private void supprimer_article_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_supprimer_article_buttonMousePressed
        int selectedRow;

        selectedRow=article_jtable.getSelectedRow();

        if (selectedRow!=-1){
            DefaultTableModel model = (DefaultTableModel)article_jtable.getModel(); //on récupère le modèle courant
            model.removeRow(selectedRow);
        }

        ajouter_auteur_button.setEnabled(false);
        ajouter_motcle_button.setEnabled (false);

        if (article_jtable.getRowCount()==0){
            valider_button.setEnabled(false);
        }

    }//GEN-LAST:event_supprimer_article_buttonMousePressed

    private void ajouter_auteur_buttonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ajouter_auteur_buttonMouseReleased
        // TODO add your handling code here:

    }//GEN-LAST:event_ajouter_auteur_buttonMouseReleased

    private void article_jtableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_article_jtableMouseReleased
        int selectedRow;
        selectedRow=article_jtable.getSelectedRow();

        if (selectedRow!=-1){
            ajouter_auteur_button.setEnabled(true);
            ajouter_motcle_button.setEnabled (true);
        } else {
            ajouter_auteur_button.setEnabled(false);
            ajouter_motcle_button.setEnabled (false);
        }
    }//GEN-LAST:event_article_jtableMouseReleased

    private void creer_parution_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_creer_parution_buttonMousePressed
        //vérification de la saisie
        String id_parution = id_parution_textfield.getText();
        Periodique per = biblio.unPeriodique(issn_textfield.getText());


        if (id_parution.isEmpty()) { //L'utilisateur n'a pas saisi d'identifiant de parution
            id_parution_notif_jlabel.setForeground(Color.red);
            id_parution_notif_jlabel.setText("Vous devez donner un identifiant de parution !");

        } else if (per.uneParution(id_parution) != null) { //l'identifiant saisi existe déjà
            id_parution_notif_jlabel.setForeground(Color.red);
            id_parution_notif_jlabel.setText("Cet identifiant existe déjà pour ce périodique.");
        } else {
        //(Dés)activation des champs nécessaire
        id_parution_notif_jlabel.setText(" ");
        article_jtable.setEnabled(true);
        ajouter_article_button.setEnabled(true);
        supprimer_article_button.setEnabled(true);
        creer_parution_button.setEnabled(false);
        id_parution_textfield.setEnabled(false);
        }

    }//GEN-LAST:event_creer_parution_buttonMousePressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CtrNouvParutionGUIllaume dialog = new CtrNouvParutionGUIllaume(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ajouter_article_button;
    private javax.swing.JButton ajouter_auteur_button;
    private javax.swing.JButton ajouter_motcle_button;
    private javax.swing.JButton annuler_button;
    private javax.swing.JTable article_jtable;
    private javax.swing.JLabel article_label;
    private javax.swing.JList auteur_jlist;
    private javax.swing.JLabel auteur_label;
    private javax.swing.JButton creer_parution_button;
    private javax.swing.JLabel id_parution_label;
    private javax.swing.JLabel id_parution_notif_jlabel;
    private javax.swing.JTextField id_parution_textfield;
    private javax.swing.JLabel issn_label;
    private javax.swing.JTextField issn_textfield;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList mot_cle_jlist;
    private javax.swing.JLabel mots_cles_label;
    private javax.swing.JLabel nom_periodique_label;
    private javax.swing.JTextField nom_periodique_textfield;
    private javax.swing.JButton rechercher_button;
    private javax.swing.JButton supprimer_article_button;
    private javax.swing.JButton valider_button;
    // End of variables declaration//GEN-END:variables

}
